package landx.toolkit.utils 
{
	import flash.geom.Point;
	/**
	 * ...
	 * @author nease@163.com
	 */
	public class CoordUtil
	{
		/**
		 * convert Stage Coordinate to Bevel
		 * @param x Coordinate x in stage
		 * @param y Coordinate y in stage
		 * @return the Point converted
		 **/
		public static function BevelToStage(x:Number, y:Number, ang:Number, isAngle:Boolean = false):Point {
			if (isAngle) {
				ang = AngleToRadian(ang);
			}
			var coord:Point=new Point();
			coord.x = (x - y) * Math.sin(ang);
			coord.y = (x + y) * Math.cos(ang);
			return coord.clone();
		}
		
		/**
		 * convert Bevel Coordinate to Stage
		 * @param x Coordinate x in Bevel
		 * @param y Coordinate y in Bevel
		 * @return the Point converted
		 **/
		public static function StageToBevel(x:Number, y:Number, ang:Number, isAngle:Boolean = false):Point {
			if (isAngle) {
				ang = AngleToRadian(ang);
			}
			var coord:Point=new Point();
			coord.x = (x / Math.sin(ang) + y / Math.cos(ang)) / 2;
			coord.y = ( -x / Math.sin(ang) + y / Math.cos(ang)) / 2;
			return coord.clone();
		}
		
		/**
		 * 				180
		 * 				|
		 * 				|
		 * 270----------------------90
		 * 				|
		 * 				|
		 * 				0
		 * @param	x
		 * @param	y
		 * @return
		 */
		public static function AngleByPoint(x:Number, y:Number):Number {
			return Math.atan2(x,y)*180/Math.PI;
		}
		
		/**
		 * Angle To Radian
		 * @param	ang
		 * @return
		 */
		public static function AngleToRadian(ang:Number):Number {
			return ang * Math.PI / 180;
		}
		
		/**
		 * Radian To Angle
		 * @param	ang
		 * @return
		 */
		public static function RadianToAngle(ang:Number):Number {
			return ang * 180 / Math.PI;
		}
		
		/**
		 * (x,y)->angle->(x1,y1)
		 * @param	x	xfrom
		 * @param	y	yfrom
		 * @param	x1	xto
		 * @param	y1	yto
		 * @return Radian bewteen two points
		 */
		public static function AngleBetween(x:Number,y:Number,x1:Number,y1:Number):Number {
			var ang2:Number = Math.atan2(x1, y1);
			var ang:Number = Math.atan2(x, y);
			return ang2 - ang;
		}
		
		/**
		 * angle [0-2PI]
		 * @param	ang
		 * @param	isAngle
		 * @return angle from 0 to 2PI
		 */
		public static function AngleIn2PI(ang:Number):Number {
			ang = ang + Math.PI * 2 * 100;
			ang = ang % (Math.PI * 2);
			return ang;
		}
		
		/**
		 * angle [0-360]
		 * @param	ang
		 * @param	isAngle
		 * @return angle from 0 to 360
		 */
		public static function AngleIn360(ang:Number):Number {
			ang = ang + 180 * 2 * 100;
			ang = ang % (180 * 2);
			return ang;
		}
		
		/**
		 * 对点的数组进行缩放 
		 * @param array	原始数组
		 * @param pozBase	参照点，这里暗指中心点
		 * @param scale	缩放值
		 * @return 新的缩放数值结合
		 */		
		public static function ScalePoints(array:Vector.<Point>,pozBase:Point,scale:Number):Vector.<Point>{
			var lines:Vector.<Point>=new Vector.<Point>();
			for(var i:int=0;i<array.length;i++){
				var p:Point=new Point();
				var c:Point=array[i];
				p.x=(c.x-pozBase.x)*scale+pozBase.x;
				p.y=(c.y-pozBase.y)*scale+pozBase.y;
				lines.push(p);
			}
			return lines;
		}
		
		/**
		 * 计算单位向量 
		 * @param ps	起始点
		 * @param pe	目标点
		 * @return 单位向量点
		 */		
		public static function UnitPoint(ps:Point,pe:Point):Point{
			var len:Number=Point.distance(ps,pe);
			var p:Point=new Point(pe.x-ps.x,pe.y-ps.y);
			if(!isZero(p)){
				p.x/=len;
				p.y/=len;
				return p;
			}
			return new Point();
		}
		
		/**
		 *判断一个点是不是原点 
		 * @param p	判断的点
		 * @return 判断结果
		 */		
		public static function isZero(p:Point):Boolean{
			return p.x==0 && p.y==0;
		}
	}
}